package conf

import (
	"fmt"
	"github.com/BurntSushi/toml"
	"github.com/golib/logger"
	"github.com/golib/mysql"
	"github.com/golib/qiniu"
	"github.com/golib/rabbitmq"
	"github.com/golib/redis"
	"github.com/golib/sms"
	"os"
	"runtime"
)

func Init(confFile string) {
	if !LoadConfig(confFile) {
		os.Exit(1)
	}
	runtime.GOMAXPROCS(runtime.NumCPU())
	logger.InitConfig(Appcfg.Logger, Appcfg.Server.Env)
	mysql.InitConfig(Appcfg.Mysql)
	redis.InitConfig(Appcfg.Redis)
	rabbitmq.InitConfig(Appcfg.RabbitMQ)
	sms.InitConfig(Appcfg.Sms)
	qiniu.InitConfig(Appcfg.Qiniu)
}

type AppConfig struct {
	Server   *Server
	Logger   *logger.LogConfig
	Mysql    *mysql.MysqlConfig
	Redis    *redis.RedisConfig
	RabbitMQ *rabbitmq.MQConf
	Sms      *sms.SmsConfig
	Qiniu    *qiniu.QiniuConfig
	Params   *Params
}

type Server struct {
	Env         string
	Listen      string
	PidFile     string
	OriginAllow []string
}

type Params struct {
	CourseSmsInternalMobileList []string
}

var Appcfg AppConfig

func LoadConfig(file string) bool {
	if _, err := toml.DecodeFile(file, &Appcfg); err != nil {
		fmt.Println(err)
		return false
	}
	return true
}
